const { request, response } = require('express')
const express = require('express')
const app = express()
app.get('/server', (request, response) => {
  // 设置响应头
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.send('Hello express get')
})
app.get('/ie-server', (request, response) => {
  // 设置响应头
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.send('Hello express ie')
})
app.post('/server',  (request, response) => {
  // 设置响应头
  response.setHeader('Access-Control-Allow-Origin', '*')
  response.send('Hello express post')
})
// 延时响应
app.get('/timeout',  (request, response) => {
  response.setHeader('Access-Control-Allow-Origin', '*')
  setTimeout(() => {
    response.send('Hello express timeout')
  }, 3000)
})
app.all('/check-username', (request, response) => {
  const data = {
    exist: 1,
    msg: '用户名已存在'
  }
  let str = JSON.stringify(data)
  let cb = request.query.callback
  // 发送一个函数请求
  response.send(`${cb}(${str})`)
})
app.listen(8000, ()=>{
  console.log('服务已启动，8000端口监听中')
})